//
//  CC.swift
//  SinaWebo
//
//  Created by 陶建 on 16/4/6.
//  Copyright © 2016年 taoJ. All rights reserved.
//

import UIKit

class CCPresentationController: UIPresentationController {
    
    var popViewFrame: CGRect = CGRectZero
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        // containerView 容器视图 this view
        // presentedView() 被展现的视图 CCPopViewController
        //　设置被展现视图的大小
        
        if popViewFrame == CGRectZero {
            popViewFrame = UIScreen.mainScreen().bounds
        }
        
        presentedView()?.frame = popViewFrame
        // 插入遮罩的View
        containerView?.insertSubview(coverView, atIndex: 0)
        
    }
    
    private lazy var coverView: UIView = {
        let coverView = UIView()
        coverView.frame = UIScreen.mainScreen().bounds
        coverView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.15)
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CCPresentationController.tapBackgroundView))
        coverView.addGestureRecognizer(tapGestureRecognizer)
        
        return coverView
    }()
    
    func tapBackgroundView() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
